package com.zr.java8.stream;

import com.zr.java8.model.Person;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;


public class FilterDemo {


    public static void main(String[] args) {
        List<Person> people = new ArrayList<>();
        List<Person> personStream = people.stream().filter(s -> s.getAge() > 0).collect(Collectors.toList());
        printPerson(personStream);

        sorted();
    }

    public static void sorted() {

        List<Integer> list = Arrays.asList(5, 4, 3, 2, 7, 1, 6, 9, 8);
        //过滤出大于5的数值
        List<Integer> filterList = list.stream().filter(x -> x > 5).collect(Collectors.toList());

        System.out.println("原始数组:" + list);
        System.out.println("过滤数组:" + filterList);


        System.out.println();

        List<Person> personList = new ArrayList<>();
        personList.add(new Person("张三", 16, 181));
        personList.add(new Person("李四", 17, 180));
        personList.add(new Person("王五", 16, 179));
        personList.add(new Person("张明", 18, 180));
        personList.add(new Person("李威", 16, 190));
        personList.add(new Person("王鹏", 15, 188));
        personList.add(new Person("王莉", 17, 177));
        personList.add(new Person("王鸥", 19, 180));

        //年龄18岁及以上，身高180的人
        //List<Person> filterPList = personList.stream().filter(s -> s.getAge() >= 18 && s.getHeight() == 180).collect(Collectors.toList());
        List<Person> filterPList = personList.stream().sorted(Comparator.comparing(Person::getAge)).collect(Collectors.toList());

        printPerson(filterPList);

    }

    public static void printPerson(List<Person> userList) {
        System.out.println("【姓名】\t【年龄】\t【身高】");
        System.out.println("-----------------------");
        userList.forEach(s -> System.out.println(s.toString()));
        System.out.println(" ");
    }
}
